package haha.life.common;

/**
 * @author cy
 * @date: 2020-12-30 17:38
 * @Version 1.0
 */

import java.util.Random;

public class RandomStringUtils {
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; ++i) {
            int number = random.nextInt(52);// [0,51)
            sb.append(str.charAt(number));
        }
        String randomStr=sb.toString();
        String starStr = randomStr.substring(0, 4);
        String endStr = randomStr.substring(4, randomStr.length());
        String millis = String.valueOf(System.currentTimeMillis());
        String returnStr=starStr+millis+endStr;
        return returnStr;
    }

    public static String getRandomString2(int length) {
        Random random = new Random();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < length; ++i) {
            int number = random.nextInt(2);
            long result = 0;

            switch (number) {
                case 0:
                    /* A-Z 的 ASCII 码值[65,90] */
                    result = Math.round(Math.random() * 25 + 65);
                    sb.append(String.valueOf((char) result));
                    break;
                case 1:
                    /* a-z 的 ASCII 码值[97,122] */
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append(String.valueOf((char) result));
                    break;
            }
        }
     return sb.toString();
    }

}

